#include "junix.h"

char *jreadline(int fd,char *buf, size_t sz)
{
	if(buf==NULL)
		err_quit("jreadline buf is NULL");

	char *p=buf, *q=buf+sz-2;
	int ch,ret;

	while(p!=q)
	{
		ret=Read(fd,&ch,1);
		if(ret==0)
		{
			if(p==buf)
				return NULL;
			break;
		}else{
			*p++=ch;
			if(ch=='\n')
				break;
		}
	}

	if( *(p-1) != '\n')
		*p++='\n';

	*p='\0';
	return buf;
}

int main(int argc, char **argv)
{
	int fd=Open("XXX",O_RDONLY);
	char buf[LINE_MAX];

	while( jreadline(fd,buf,LINE_MAX) != NULL)
		printf("%s",buf);

	exit(0);
}

